package me.zijunlin.sms.save.domain;

import java.util.List;


import me.zijunlin.sms.save.dao.SmsDao;
import me.zijunlin.sms.save.dao.XmlHelp;
import me.zijunlin.sms.save.model.Person;
import me.zijunlin.sms.save.model.Sms;
import android.content.Context;
import android.database.Cursor;

public class XmlSmsHandler extends SmsHandler {

	private final String FILE_NAME="Sms.xml";
	public XmlSmsHandler(DbConverter converter, Context context) {
		super(converter, context);

	}

	@Override
	public List<Sms> Query() {
		SmsDao dao = new SmsDao(context);
		Cursor cursor = dao.querySmsFromInbox();
		List<Sms> list= converter.ConvertToSmsList(cursor);
		
		for(Sms item:list)
		{
			String phoneNumber=item.getPhoneNumber();
			if(phoneNumber!=null)
			{
			  cursor=dao.queryPersonByPhoneNum(phoneNumber);
			  Person person=converter.ConvertToPerson(cursor);
			  item.setPerson(person);
			}
		}
	
		return list;
	}
	
	

	@Override
	public void Save(List<Sms> list) {
		XmlHelp xmlHelp=new XmlHelp();
		xmlHelp.formatToXml(list);
		xmlHelp.Write(path,FILE_NAME, context);

	}

}
